/**
 * Created by 37 on 2017/2/27.
 */

// post
function ajax_post(data, url, successfn, dataType) {
    $.ajax({
        type: "POST",
        url: url,       //提交的URL
        data: data,
        dataType: dataType,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (json) {
            successfn(json);
        },
        error: function (request) {
            $.error("Connection error");
        }
    });
}

// get
function ajax_get(data, url, successfn, dataType) {
    $.ajax({
        type: "GET",
        url: url,   //提交的URL
        data: data,
        dataType: dataType,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (json) {
            successfn(json);
        },
        error: function (request) {
            $.error("Connection error");
        }
    });
}

function header() {
  var header = {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
  return header;
}

/**
 * 判断变量是否为空
 * @param {Number}{String} data
 */
function isNull(data) {
    if (data == "" || data == undefined || data == null || data == "false") {
        return true;
    } else {
        return false;
    }
}

/**
 * 对象的结构转化成key-value结构
 * @param   object
 * @param   k
 * @return  object
 */
function convertObjToKeyValue(object,key) {
    var kvObject = {};
    if(!isNull(object) && !$.isEmptyObject(object)) {
        $.each(object,function(index,item){
            if(!isNull(item[key])) {
                kvObject[item[key]] = item;
            }
        })
    }
    return kvObject;
}
